package com.hotelsystem.service.impl;

import com.hotelsystem.dao.*;
import com.hotelsystem.pojo.*;
import com.hotelsystem.service.OrdersService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.*;

@Service
@Transactional //开启事务的支持，默认为增删改提交
public class OrdersServiceImpl implements OrdersService {

    @Resource(name = "ordersMapper")
    private OrdersMapper ordersMapper;

    @Resource(name = "orderDetailMapper")
    private OrderDetailMapper orderDetailMapper;

    @Resource(name = "statusMapper")
    private StatusMapper statusMapper;

    @Resource(name = "roomTypeMapper")
    private RoomTypeMapper roomTypeMapper;

    @Resource(name = "roomMapper")
    private RoomMapper roomMapper;

    @Resource(name = "customerMapper")
    private CustomerMapper customerMapper;

    @Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
    @Override
    public List<Orders> list(Map<String,Object> map) throws Exception {
        return ordersMapper.list(map);
    }

    @Override
    public Integer updateOrderStatus(String roomId,Integer roomTypeId,String ordersId, String statusName) throws Exception {
        //修改房型信息和房间状态
        roomMapper.updateStatusByRoomId(roomId,4);
        RoomType roomType = roomTypeMapper.get(roomTypeId);
        roomType.setUsable(roomType.getUsable() + 1);
        roomType.setReserved(roomType.getReserved() - 1);
        if (roomType.getStatus() == 0) roomType.setStatus(1);
        roomTypeMapper.updateState(roomType);
        Status status = statusMapper.get(statusName);
        if (status != null) {
            return ordersMapper.update(ordersId,status.getStatusId());
        }
        return 0;
    }

    @Override
    public Integer insert(Orders orders) throws Exception {
        //判断当前客户是否存在如果不存在先添加用户在添加订单
        Customer customer = customerMapper.get(orders.getCustomer().getIdNumber());
        if (customer == null){
            customerMapper.insert(orders.getCustomer());
            customer = orders.getCustomer();
        }
        orders.setOrdersId(UUID.randomUUID().toString().replaceAll("-",""));
        orders.setCustomer(customer);
        orders.setStatus(new Status());
        orders.getStatus().setStatusId(2);
        //修改房型信息和房间状态
        roomMapper.updateStatusByRoomId(orders.getRoom().getRoomId(),2);
        //查询当前预订的房型信息
        RoomType roomType = roomTypeMapper.get(orders.getRoom().getRoomType().getRoomTypeId());
        //修改房型相关信息
        roomType.setUsable(roomType.getUsable() - 1);
        //如果可用房间数为0表示该房型下的房间全部被预订或入住，将状态改为0：房间已满
        if (roomType.getUsable() == 0){
            roomType.setStatus(0);
        }
        roomType.setReserved(roomType.getReserved() + 1);
        roomTypeMapper.updateState(roomType);
        ordersMapper.insert(orders);//添加订单
        //添加订单明细
        return orderDetailMapper.insert(new OrderDetail(null,orders,null,null,null,null));
    }

    @Override
    public Integer insertCheckInRegister(Orders orders,OrderDetail orderDetail) throws Exception {
        //1、根据身份证号查询用户是否存在，不存在则添加,存在不添加
        Customer customer = customerMapper.get(orders.getCustomer().getIdNumber());
        if (customer == null){
            customerMapper.insert(orders.getCustomer());
            customer = orders.getCustomer();
        }else{
            //如果存在则修改客户信息
            Customer cus = orders.getCustomer();
            cus.setCustomerId(customer.getCustomerId());
            customerMapper.update(cus);
            customer = cus;
        }
        //共用的数据设置
        orders.setCustomer(customer);
        //获取离店时间来设置房间释放时间，默认为离店时间的后两个小时
        orders.setLiberateDate(new Date(orders.getCheckOutDate().toString()));
        orders.getLiberateDate().setHours(orders.getLiberateDate().getHours() + 2);
        //修改为入住状态
        orders.setStatus(new Status());
        orders.getStatus().setStatusId(1);
        //2、根据该客户是否有预订房间来修改房型信息和房间状态，由于前台的房间号已经判断好了此处直接修改即可
        roomMapper.updateStatusByRoomId(orders.getRoom().getRoomId(),1);//修改房间状态
        //如果预订时间不为空表示该客户预订过房间，直接根据房间号找到对应房型改掉原来的房型号，房间号已经在前面做了验证
        //如果预订时间为空表示该客户没有预订过房间，直接使用该用户选择的房型
        if (orders.getReservedDate() != null){
            Room room = roomMapper.getRoomInfoWithId(orders.getRoom().getRoomId());
            orders.getRoom().getRoomType().setRoomTypeId(room.getRoomType().getRoomTypeId());
        }
        RoomType roomType = roomTypeMapper.get(orders.getRoom().getRoomType().getRoomTypeId());
        roomType.setCheckIn(roomType.getCheckIn() + 1);
        //查询当前客户有没有预订信息如果有直接修改订单
        Map<String, Object> map = new HashMap<>();
        map.put("idNumber",orders.getCustomer().getIdNumber());
        map.put("status", Arrays.asList("2"));
        List<Orders> list = ordersMapper.list(map);
        if (list.size() == 0){
            //如果没有预订房间可用数要减1
            roomType.setUsable(roomType.getUsable() - 1);
            //如果可用房间数为0表示该房型下的房间全部被预订或入住，将状态改为0：房间已满
            if (roomType.getUsable() == 0){
                roomType.setStatus(0);
            }
            //3、该客户没有预订过房间则添加订单信息和订单明细
            orders.setOrdersId(UUID.randomUUID().toString().replaceAll("-",""));
            //添加订单
            ordersMapper.insert(orders);
            //添加订单明细
            orderDetail.setOrders(orders);
            orderDetailMapper.insert(orderDetail);
        }else{
            //如果有预订房间可用数则不需要减
            roomType.setReserved(roomType.getReserved() - 1);
            //4、如果该客户已经预订过房间直接修改订单信息和订单明细
            //获取原有的预订信息
            Orders order = list.get(0);
            orders.setOrdersId(order.getOrdersId());
            ordersMapper.updateAll(orders);//修改订单
            List<OrderDetail> orderDetails = orderDetailMapper.list(orders.getOrdersId());
            orderDetail.setOrderDetailId(orderDetails.get(0).getOrderDetailId());
            orderDetailMapper.update(orderDetail);//修改订单明细
        }
        //修改房型信息
        return roomTypeMapper.updateState(roomType);
    }

    @Override
    public Integer delete(String ordersId) throws Exception {
        orderDetailMapper.delete(ordersId); //删除订单明细
        return ordersMapper.delete(ordersId); //删除订单
    }

    @Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
    @Override
    public List<Orders> listByOrdersStatus(String idNumber) throws Exception {
        return ordersMapper.listByOrdersStatus(idNumber);
    }

    @Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
    @Override
    public List<Orders> listByOrdersId(String ordersId) throws Exception {
        return ordersMapper.listByOrdersId(ordersId);
    }

    @Override
    public Integer updateAll(Orders orders) throws Exception {
        return ordersMapper.updateAll(orders);
    }

    @Override
    public Integer checkOut(String ordersId, String roomId, Integer roomTypeId) throws Exception {
        //1、修改房间状态为打扫中
        roomMapper.updateStatusByRoomId(roomId,5);
        //2、修改房型入住数减1
        RoomType roomType = roomTypeMapper.get(roomTypeId);
        roomType.setCheckIn(roomType.getCheckIn() - 1);
        roomTypeMapper.updateState(roomType);
        //3、修改订单状态为已结算离店
        return ordersMapper.update(ordersId,8);
    }

    @Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
    @Override
    public List<Orders> getShuJuTongJiByMoth(Timestamp min, Timestamp max) {
        return ordersMapper.selectShuJuTongJi(min,max);
    }
}